package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbCpuStandard;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbCpuStandardRepository extends JpaRepository<CmdbCpuStandard, Integer>, JpaSpecificationExecutor<CmdbCpuStandard> {
    List<CmdbCpuStandard> findAllByIdIn(List<Long> id);

    @Query(value = "select c from CmdbCpuStandard c where c.isDelete = 0 " +
            "and (:#{#paramSearchDto.cpuStandardCode} is null OR c.cpuStandardCode LIKE CONCAT('%', :#{#paramSearchDto.cpuStandardCode},'%') ) " +
            "and (:#{#paramSearchDto.name} is null OR c.name LIKE CONCAT('%',:#{#paramSearchDto.name},'%') ) " +
            "and (:#{#paramSearchDto.brand} is null OR c.brand = :#{#paramSearchDto.brand}) " +
            "and (:#{#paramSearchDto.partNum} is null OR c.partNum LIKE CONCAT('%',:#{#paramSearchDto.partNum},'%') ) " +
            "and (:#{#paramSearchDto.standardNum} is null OR c.standardNum LIKE CONCAT('%',:#{#paramSearchDto.standardNum},'%') ) " +
            "and (:#{#paramSearchDto.hz} is null OR c.hz LIKE CONCAT('%',:#{#paramSearchDto.hz},'%') ) " +
            "and (:#{#paramSearchDto.core} is null OR c.core = :#{#paramSearchDto.core}) " +
            "and (:#{#paramSearchDto.smt} is null OR c.smt = :#{#paramSearchDto.smt}) ")
    Page<CmdbCpuStandard> searchByParam(Pageable pageable, CmdbCpuStandard paramSearchDto);


    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_cpu_standard set is_delete = :deleted where cpu_standard_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);

    CmdbCpuStandard findOneByCpuStandardCode(String cpuStandardCode);

    List<CmdbCpuStandard> findAllByNameAndIsDelete(String name, Integer noDelete);

    List<CmdbCpuStandard> findAllByPartNumAndIsDelete(String partNum, Integer noDelete);
}

